/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.xxl.adapter.controller;

import cn.star.framework.core.api.AppResult;
import com.xxl.job.admin.controller.JobInfoController;
import com.xxl.job.admin.controller.JobLogController;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.model.XxlJobLogGlue;
import com.xxl.job.admin.core.model.XxlJobUser;
import com.xxl.job.admin.core.util.I18nUtil;
import com.xxl.job.admin.dao.XxlJobInfoDao;
import com.xxl.job.admin.dao.XxlJobLogDao;
import com.xxl.job.admin.dao.XxlJobLogGlueDao;
import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.glue.GlueTypeEnum;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * JobCodeController
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@RestController
@RequestMapping("/xxl/adapter")
public class AdapterController {
  @Resource private XxlJobInfoDao xxlJobInfoDao;
  @Resource private XxlJobLogGlueDao xxlJobLogGlueDao;
  @Resource private XxlJobLogDao xxlJobLogDao;
  @Resource
  private XxlJobService xxlJobService;
  @Autowired private JobLogController jobLogController;
  @Autowired private JobInfoController jobInfoController;

  @RequestMapping({"/report/dashboard"})
  public AppResult<Map<String, Object>> IndexController_index() {
    Map<String, Object> dashboardMap = xxlJobService.dashboardInfo();

    return AppResult.ofSuccess(dashboardMap);
  }

  @RequestMapping({"/jobcode"})
  public AppResult<List<XxlJobLogGlue>> JobCodeController_index(
      HttpServletRequest request, int id) {
    try {
      XxlJobInfo jobInfo = xxlJobInfoDao.loadById(id);
      List<XxlJobLogGlue> jobLogGlues = xxlJobLogGlueDao.findByJobId(id);

      if (jobInfo == null) {
        throw new RuntimeException(I18nUtil.getString("jobinfo_glue_jobid_unvalid"));
      }
      if (GlueTypeEnum.BEAN == GlueTypeEnum.match(jobInfo.getGlueType())) {
        throw new RuntimeException(I18nUtil.getString("jobinfo_glue_gluetype_unvalid"));
      }
      return AppResult.ofSuccess(jobLogGlues);
    } catch (Exception e) {
      return AppResult.ofFail(e.getMessage());
    }
  }

  @RequestMapping({"/jobinfo/trigger"})
  @ResponseBody
  public ReturnT<String> JobInfoController_triggerJob(
      HttpServletRequest request, int id, String executorParam, String addressList) {
    this.wrapper(request);

    return this.jobInfoController.triggerJob(request, id, executorParam, addressList);
  }

  @RequestMapping("/joblog/pageList")
  @ResponseBody
  public Map<String, Object> JobLogController_pageList(
      HttpServletRequest request,
      @RequestParam(required = false, defaultValue = "0") int start,
      @RequestParam(required = false, defaultValue = "10") int length,
      int jobGroup,
      int jobId,
      int logStatus,
      String filterTime) {
    this.wrapper(request);

    return jobLogController.pageList(
        request, start, length, jobGroup, jobId, logStatus, filterTime);
  }

  private void wrapper(HttpServletRequest request) {
    XxlJobUser loginUser = (XxlJobUser) request.getAttribute("XXL_JOB_LOGIN_IDENTITY");
    if (loginUser == null) {
      loginUser = new XxlJobUser();
      loginUser.setRole(1);
      request.setAttribute("XXL_JOB_LOGIN_IDENTITY", loginUser);
    }
  }
}
